using System;
using System.Collections;
using System.Reflection;
using DataBroker.Dao;

namespace ScottDAO
{
    ///<summary>
    /// DAO Class for the DEPT table.
    ///</summary>
    public class DEPT : Entity
    {
        private Int32 _DEPTNO;
        private String _DNAME;
        private String _LOC;
	
        ///Default constructor
        public DEPT ()
        {
		
        }

        /// <summary>
        /// Constructor for joins. 
        /// Available CHILD tables to join with:
        ///    - EMP ( EMP_FK_DEPTNO )
        /// </summary>
        public DEPT(bool Flat, params Entity[] entities)
            : this()
        {
            foreach(Entity entity in entities)
            {
                string[] entityParts = entity.GetType().ToString().Split('.');
                string entityName = entityParts[entityParts.Length - 1];
                PropertyInfo pf = GetType().GetProperty((entityName));
                if (pf != null)
                {
                    pf.SetValue(this,new EntityCollection(entity), null);
                }
                entityName = entityName + "_";
                pf = GetType().GetProperty((entityName));
                if (pf != null)
                {
                    pf.SetValue(this, entity, null);
                }
            }
            SetFlat(Flat);
        }

        ///Constructor for Joins with flat hierarchy
        public DEPT(params Entity[] entities)
            : this(true, entities)
        {
        }
	
        ///<summary>
        /// Property for column DEPTNO.
        ///</summary>
        [IsNotNullable]
        [IsPK]
        public Int32 DEPTNO
        {
            get
            {
                return _DEPTNO;
            }
            set
            {
                _DEPTNO = value;
            }
        }
        ///<summary>
        /// Property for column DNAME.
        ///</summary>
        [MaxLength(14)]
        public String DNAME
        {
            get
            {
                return _DNAME;
            }
            set
            {
                _DNAME = value;
            }
        }
        ///<summary>
        /// Property for column LOC.
        ///</summary>
        [MaxLength(13)]
        public String LOC
        {
            get
            {
                return _LOC;
            }
            set
            {
                _LOC = value;
            }
        }
	
        #region EMP 
        private EntityCollection _EMP;
        ///<summary>
        /// Related table EMP as a collection for hierarchical retrieval.
        ///</summary>
        [NotInTable]
        [TypeContainedAttribute(typeof(EMP),"EMP")]
        public EntityCollection EMP
        {
            get
            {
                return _EMP;
            }

            set
            {
                _EMP = value;
            }
        }
        private EMP _EMP_;
        ///<summary>
        /// Related table EMP as a single object for flat retrieval.
        ///</summary>
        [NotInTable]
        public EMP EMP_
        {
            get
            {
                return _EMP_;
            }

            set
            {
                _EMP_ = value;
            }
        }
        #endregion
        /// <summary>
        /// Gets join for current entity and table.
        /// </summary>
        /// <param name="tableName">Table to join with entity.</param>
        /// <returns>Join definition for entity and table.</returns>
        public override DBJoin GetJoin(string tableName)
        {
            if (string.Compare(tableName, "EMP", StringComparison.OrdinalIgnoreCase) == 0)
            {
                ArrayList left = new ArrayList();
                ArrayList right = new ArrayList();
                left.Add(tableName + ".DEPTNO");
                right.Add(GetType().Name + ".DEPTNO");
                return new DBJoin(left, right, JoinTypeEnumeration.LEFT_OUTER);
            }
            return null;
        }
    }
}